﻿using System;

namespace SimpleServiceBus.Bus.MessageManagement
{
    [Serializable]
    public class MessageTypePredicate : IMessagePredicate
    {
        public Type MessageType { get; set; }

        #region IMessagePredicate Members

        public bool Matches(MessageEnvelope messageEnvelope)
        {
            //typeof(object) is considered a catchall, so it can match even a null message body
            if (messageEnvelope == null || messageEnvelope.Body == null)
                return MessageType == null || MessageType == typeof(object);

            if (MessageType == null)
                return false;

            return MessageType.IsAssignableFrom(messageEnvelope.Body.GetType());
        }

        #endregion
    }
}